<!DOCTYPE html>




<html class="theme-next gemini" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.3">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="Android测试,自动化测试,Android Monkey Runner," />





  <link rel="alternate" href="/atom.xml" title="流水不腐小夏" type="application/atom+xml" />






<meta name="description" content="Android monkeyrunner整理 [TOC] 简介monkeyrunner即android SDK中自带的工具之一，此工具提供API可按制android设备或模拟器。monkeyrunner提供了一个API，使用此API写出的程序可以在Android代码之外控制Android设备和模拟器。通过monkeyrunner，您可以写出一个Python程序去安装一个Android应用程序或测试">
<meta name="keywords" content="Android测试,自动化测试,Android Monkey Runner">
<meta property="og:type" content="article">
<meta property="og:title" content="AndroidMonkeyRunner">
<meta property="og:url" content="http://mycommons.cn/2016/07/25/AndroidMonkeyRunner/index.html">
<meta property="og:site_name" content="流水不腐小夏">
<meta property="og:description" content="Android monkeyrunner整理 [TOC] 简介monkeyrunner即android SDK中自带的工具之一，此工具提供API可按制android设备或模拟器。monkeyrunner提供了一个API，使用此API写出的程序可以在Android代码之外控制Android设备和模拟器。通过monkeyrunner，您可以写出一个Python程序去安装一个Android应用程序或测试">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2016-07-25T13:25:05.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="AndroidMonkeyRunner">
<meta name="twitter:description" content="Android monkeyrunner整理 [TOC] 简介monkeyrunner即android SDK中自带的工具之一，此工具提供API可按制android设备或模拟器。monkeyrunner提供了一个API，使用此API写出的程序可以在Android代码之外控制Android设备和模拟器。通过monkeyrunner，您可以写出一个Python程序去安装一个Android应用程序或测试">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.3',
    sidebar: {"position":"left","display":"always","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":false,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: 'GRLHBBCD9E',
      apiKey: '759ab3f5c09b132b3ed4842a44c53c45',
      indexName: 'mycommons',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://mycommons.cn/2016/07/25/AndroidMonkeyRunner/"/>





  <title>AndroidMonkeyRunner | 流水不腐小夏</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?55458ffca1f60dd283f11b9a7f82b262";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">流水不腐小夏</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">http://mycommons.cn</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  
  <div class="algolia-popup popup search-popup">
    <div class="algolia-search">
      <div class="algolia-search-input-icon">
        <i class="fa fa-search"></i>
      </div>
      <div class="algolia-search-input" id="algolia-search-input"></div>
    </div>

    <div class="algolia-results">
      <div id="algolia-stats"></div>
      <div id="algolia-hits"></div>
      <div id="algolia-pagination" class="algolia-pagination"></div>
    </div>

    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
  </div>




    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://mycommons.cn/2016/07/25/AndroidMonkeyRunner/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="流水不腐小夏">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="流水不腐小夏">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">AndroidMonkeyRunner</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2016-07-25T21:22:56+08:00">
                2016-07-25
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android测试/" itemprop="url" rel="index">
                    <span itemprop="name">Android测试</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2016/07/25/AndroidMonkeyRunner/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2016/07/25/AndroidMonkeyRunner/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv">本文总阅读量
            <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>次
            </span>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="Android-monkeyrunner整理"><a href="#Android-monkeyrunner整理" class="headerlink" title="Android monkeyrunner整理"></a>Android monkeyrunner整理</h1><hr>
<p>[TOC]</p>
<h1 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h1><p>monkeyrunner即android SDK中自带的工具之一，此工具提供API可按制android设备或模拟器。<br>monkeyrunner提供了一个API，使用此API写出的程序可以在Android代码之外控制Android设备和模拟器。通过monkeyrunner，您可以写出一个Python程序去安装一个Android应用程序或测试包，运行它，向它发送模拟击键，截取它的用户界面图片，并将截图存储于工作站上。<br>monkeyrunner工具的主要目的是用于测试功能/框架水平上的应用程序和设备，或用于运行单元测试套件，但您当然也可以将其用于其它目的。</p>
<h1 id="特性"><a href="#特性" class="headerlink" title="特性"></a>特性</h1><ul>
<li><p>多设备控制：monkeyrunner API可以跨多个设备或模拟器实施测试套件。您可以在同一时间接上所有的设备或一次启动全部模拟器（或统统一起），依据程序依次连接到每一个，然后运行一个或多个测试。您也可以用程序启动一个配置好的模拟器，运行一个或多个测试，然后关闭模拟器。</p>
</li>
<li><p>功能测试：monkeyrunner可以为一个应用自动化功能测试。为您提供按键或触摸事件的输入数值，然后观察输出结果的截屏。       </p>
</li>
<li><p>回归测试：monkeyrunner可以运行某个应用，并将其结果截屏与既定已知正确的结果截屏相比较，以此测试应用的稳定性。       </p>
</li>
<li><p>可扩展的自动化：由于monkeyrunner是一个API工具包，您可以基于Python模块和程序开发一整套系统，以此来控制Android设备。除了使用monkeyrunner    API之外，您还可以使用标准的Python    os和subprocess模块来调用如adb这样的Android工具。    </p>
</li>
</ul>
<h1 id="使用方法"><a href="#使用方法" class="headerlink" title="使用方法"></a>使用方法</h1><h2 id="Monkeyrunner-API"><a href="#Monkeyrunner-API" class="headerlink" title="Monkeyrunner API"></a>Monkeyrunner API</h2><p>主要包括三个模块<br>1、MonkeyRunner:这个类提供了用于连接monkeyrunner和设备或模拟器的方法，它还提供了用于创建用户界面显示提供了方法。<br>2、MonkeyDevice:代表一个设备或模拟器。这个类为安装和卸载包、开启Activity、发送按键和触摸事件、运行测试包等提供了方法。<br>3、MonkeyImage:这个类提供了捕捉屏幕的方法。这个类为截图、将位图转换成各种格式、对比两个MonkeyImage对象、将image保存到文件等提供了方法。</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#引用导入API</span></div><div class="line"><span class="keyword">from</span> com.android.monkeyrunner <span class="keyword">import</span> &lt;module&gt;</div></pre></td></tr></table></figure>
<h2 id="运行"><a href="#运行" class="headerlink" title="运行"></a>运行</h2><p>命令语法为：</p>
<figure class="highlight bat"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div></pre></td><td class="code"><pre><div class="line">monkeyrunner -plugin &lt;plugin_jar&gt; &lt;program_filename&gt; &lt;program_options&gt;</div><div class="line">``` </div><div class="line">方式一：在<span class="built_in">CMD</span>命令窗口直接运行monkeyrunner</div><div class="line">方式二：使用Python编写测试代码文件，在<span class="built_in">CMD</span>中执行monkeyrunner Findyou.py运行</div><div class="line">不论使用哪种方式，您都需要调用SDK目录的tools子目录下的monkeyrunner命令。</div><div class="line"></div><div class="line">**注意**</div><div class="line">在运行monkeyrunner之前必须先运行相应的模拟器或连接真机，否则monkeyrunner无法连接到设备</div><div class="line"></div><div class="line">运行模拟器有两种方法：</div><div class="line"><span class="number">1</span>、通过eclipse中执行模拟器</div><div class="line"> <span class="number">2</span>、在<span class="built_in">CMD</span>中通过命令调用模拟器</div><div class="line">这里介绍通过命令，在<span class="built_in">CMD</span>中执行模拟器的方法</div><div class="line">`emulator -avd test`</div><div class="line">上面命令中test是指模拟器的名称。</div><div class="line"> </div><div class="line">附：</div><div class="line">问题：<span class="built_in">CMD</span>运行提示monkeyrunner不是内部或外部命令，也不是可运行的程序或批处理文件。</div><div class="line">解决：电脑环境变量未配置，将monkeyrunner所在目录配在环境变量里。</div><div class="line">变量名：<span class="built_in">Path</span></div><div class="line">变量值：`$&#123;ANDROID_HOME&#125;\platform-tools`</div><div class="line"></div><div class="line"># 示例——卸载旧的APP，安装新的APP</div><div class="line">**准备**</div><div class="line">a. 连接安卓真机设备</div><div class="line">b. 运行<span class="built_in">CMD</span>，检测是否连接成功</div><div class="line">**命令**</div><div class="line"></div><div class="line">```sh</div><div class="line">adb devices</div></pre></td></tr></table></figure>
<h2 id="方法一"><a href="#方法一" class="headerlink" title="方法一"></a>方法一</h2><p>1.打开CMD，运行monkeyrunner<br>2.进入monkeyrunner的shell命令交互模式后，逐条输入以下命令</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">from</span> com.android.monkeyrunner <span class="keyword">import</span> MonkeyRunner, MonkeyDevice, MonkeyImage</div><div class="line">device = MonkeyRunner.waitForConnection()</div><div class="line">device.removePackage(<span class="string">'xx.oo.package'</span>)</div><div class="line">device.installPackage(<span class="string">'xx.oo.apk'</span>)</div></pre></td></tr></table></figure>
<p>注：每条命令的作用，请见方法二中的注解</p>
<h2 id="方法二"><a href="#方法二" class="headerlink" title="方法二"></a>方法二</h2><ul>
<li>编写Python测试代码</li>
</ul>
<figure class="highlight py"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># 引入本程序所用到的模块</span></div><div class="line"><span class="keyword">from</span> com.android.monkeyrunner <span class="keyword">import</span> MonkeyRunner, MonkeyDevice, MonkeyImage</div><div class="line"></div><div class="line"><span class="comment"># 连接手机设备</span></div><div class="line">device = MonkeyRunner.waitForConnection()</div><div class="line"></div><div class="line"><span class="comment"># 截图</span></div><div class="line">result = device.takeSnapshot()</div><div class="line"></div><div class="line"><span class="comment"># 将截图保存到文件 </span></div><div class="line">result.writeToFile(<span class="string">'./shot01.png'</span>,<span class="string">'png'</span>)</div><div class="line"></div><div class="line"><span class="comment"># 卸载APP</span></div><div class="line">device.removePackage(<span class="string">'xx.oo.package'</span>)</div><div class="line"><span class="keyword">print</span> (<span class="string">'Uninstall xx.oo.package Success!'</span>)</div><div class="line"></div><div class="line"><span class="comment"># 暂停5秒</span></div><div class="line">MonkeyRunner.sleep(<span class="number">5</span>)</div><div class="line"></div><div class="line"><span class="comment"># 截图</span></div><div class="line">result = device.takeSnapshot()</div><div class="line">result.writeToFile(<span class="string">'./shot02.png'</span>,<span class="string">'png'</span>)</div><div class="line"></div><div class="line"><span class="comment"># 安装新的APP</span></div><div class="line">device.installPackage(<span class="string">'xx.oo.apk'</span>)</div><div class="line"><span class="keyword">print</span> (<span class="string">'Install xx.oo.apk Success!'</span>)</div><div class="line"></div><div class="line"><span class="comment"># 截图</span></div><div class="line">result = device.takeSnapshot()</div><div class="line">result.writeToFile(<span class="string">'./shot03.png'</span>,<span class="string">'png'</span>)</div></pre></td></tr></table></figure>
<p><strong>注：</strong>拷贝运行时请去掉中文注释；或者在开头加入 #coding=utf-8 </p>
<ul>
<li>运行脚本</li>
</ul>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">Monkeyrunner Test1.py</div></pre></td></tr></table></figure>
<ul>
<li>检查手机app是否已更新</li>
</ul>
<h1 id="API"><a href="#API" class="headerlink" title="API"></a>API</h1><p>Monkey Runner主要API有下面三种，代码package为<code>com.android.monkeyrunner</code>。</p>
<ul>
<li>MonkeyRunner<br>弹出警告框、选择列表、帮助文档、输入、暂停、等待连接设备等。</li>
<li>MonkeyDevice<br>主要操作设备的，包含安装、卸载，发送广播，启动应用等。</li>
<li>MonkeyImage<br>主要操作图片，包含图片保存、对比、格式转化、获取像素点等。</li>
</ul>
<h2 id="MonkeyRunner"><a href="#MonkeyRunner" class="headerlink" title="MonkeyRunner"></a>MonkeyRunner</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">alert</span> <span class="params">(string message, string title, string okTitle)</span></span></div><div class="line"><span class="function"><span class="comment">// Displays an alert dialog to the process running the current program.</span></span></div><div class="line"><span class="function"><span class="comment">// 弹出一个确认框，在pc上面</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function">integer	<span class="title">choice</span> <span class="params">(string message, iterable choices, string title)</span></span></div><div class="line"><span class="function"><span class="comment">// Displays a dialog with a list of choices to the process running the current program.</span></span></div><div class="line"><span class="function"><span class="comment">// 弹出一个选择框，在pc上面</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">help</span> <span class="params">(string format)</span></span></div><div class="line"><span class="function"><span class="comment">// Displays the monkeyrunner API reference in a style similar to that of Python's pydoc tool, using the specified format.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function">string	<span class="title">input</span> <span class="params">(string message, string initialValue, string title, string okTitle, string cancelTitle)</span></span></div><div class="line"><span class="function"><span class="comment">// Displays a dialog that accepts input.</span></span></div><div class="line"><span class="function"><span class="comment">// 弹出一个输入框，在pc上面</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">sleep</span> <span class="params">(<span class="keyword">float</span> seconds)</span></span></div><div class="line"><span class="function"><span class="comment">// Pauses the current program for the specified number of seconds.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function">MonkeyDevice	<span class="title">waitForConnection</span> <span class="params">(<span class="keyword">float</span> timeout, string deviceId)</span></span></div><div class="line"><span class="function"><span class="comment">// Tries to make a connection between the monkeyrunner backend and the specified device or emulator.</span></span></div><div class="line"><span class="function"><span class="comment">// 待定设备连接</span></span></div></pre></td></tr></table></figure>
<h2 id="MonkeyDevice"><a href="#MonkeyDevice" class="headerlink" title="MonkeyDevice"></a>MonkeyDevice</h2><p>可以使用<code>newdevice = MonkeyRunner.waitForConnection()</code>获取MonkeyDevice对象</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">broadcastIntent</span> <span class="params">(string uri, string action, string data, string mimetype, iterable categories dictionary extras, component component, iterable flags)</span></span></div><div class="line"><span class="function"><span class="comment">//Broadcasts an Intent to this device, as if the Intent were coming from an application.</span></span></div><div class="line"><span class="function"><span class="comment">// 向设备发送一个广播</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">drag</span> <span class="params">(tuple start, tuple end, <span class="keyword">float</span> duration, integer steps)</span></span></div><div class="line"><span class="function"><span class="comment">// Simulates a drag gesture (touch, hold, and move) on this device's screen.</span></span></div><div class="line"><span class="function"><span class="comment">// 拖动</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function">object	<span class="title">getProperty</span> <span class="params">(string key)</span></span></div><div class="line"><span class="function"><span class="comment">//Given the name of a system environment variable, returns its value for this device. The available variable names are listed in the detailed description of this method.</span></span></div><div class="line"><span class="function"><span class="comment">//object	getSystemProperty (string key)</span></span></div><div class="line"><span class="function"><span class="comment">//The API equivalent of adb shell getprop &lt;key&gt;. This is provided for use by platform developers.</span></span></div><div class="line"><span class="function"><span class="comment">// 获取一个属性</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">installPackage</span> <span class="params">(string path)</span></span></div><div class="line"><span class="function"><span class="comment">//Installs the Android application or test package contained in packageFile onto this device. If the application or test package is already installed, it is replaced.</span></span></div><div class="line"><span class="function"><span class="comment">//dictionary	instrument (string className, dictionary args)</span></span></div><div class="line"><span class="function"><span class="comment">//Runs the specified component under Android instrumentation, and returns the results in a dictionary whose exact format is dictated by the component being run. The component must already be present on this device.</span></span></div><div class="line"><span class="function"><span class="comment">// 安装一个apk，参数是apk的路径</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">press</span> <span class="params">(string name, dictionary type)</span></span></div><div class="line"><span class="function"><span class="comment">//Sends the key event specified by type to the key specified by keycode.</span></span></div><div class="line"><span class="function"><span class="comment">// 发送一个按钮事件</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">reboot</span> <span class="params">(string into)</span></span></div><div class="line"><span class="function"><span class="comment">//Reboots this device into the bootloader specified by bootloadType.</span></span></div><div class="line"><span class="function"><span class="comment">// 重启设备</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">removePackage</span> <span class="params">(string <span class="keyword">package</span>)</span></span></div><div class="line"><span class="function"><span class="comment">//Deletes the specified package from this device, including its data and cache.</span></span></div><div class="line"><span class="function"><span class="comment">//根据apk的package删除app</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function">object	<span class="title">shell</span> <span class="params">(string cmd)</span></span></div><div class="line"><span class="function"><span class="comment">//Executes an adb shell command and returns the result, if any.</span></span></div><div class="line"><span class="function"><span class="comment">// 执行一个shell命令</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">startActivity</span> <span class="params">(string uri, string action, string data, string mimetype, iterable categories dictionary extras, component component, flags)</span></span></div><div class="line"><span class="function"><span class="comment">//Starts an Activity on this device by sending an Intent constructed from the supplied arguments.</span></span></div><div class="line"><span class="function"><span class="comment">//MonkeyImage	takeSnapshot()</span></span></div><div class="line"><span class="function"><span class="comment">//Captures the entire screen buffer of this device, yielding a MonkeyImage object containing a screen capture of the current display.</span></span></div><div class="line"><span class="function"><span class="comment">// 启动一个activity</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">touch</span> <span class="params">(integer x, integer y, integer type)</span></span></div><div class="line"><span class="function"><span class="comment">//Sends a touch event specified by type to the screen location specified by x and y.</span></span></div><div class="line"><span class="function"><span class="comment">// 发送触摸事件</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">type</span> <span class="params">(string message)</span></span></div><div class="line"><span class="function"><span class="comment">//Sends the characters contained in message to this device, as if they had been typed on the device's keyboard. This is equivalent to calling press() for each keycode in message using the key event type DOWN_AND_UP.</span></span></div><div class="line"><span class="function"><span class="comment">// 向设备输入一些字符串</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">wake</span> <span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">//Wakes the screen of this device.</span></span></div><div class="line"><span class="function"><span class="comment">// 唤醒设备</span></span></div></pre></td></tr></table></figure>
<h2 id="MonkeyImage"><a href="#MonkeyImage" class="headerlink" title="MonkeyImage"></a>MonkeyImage</h2><p>使用<code>newimage = MonkeyDevice.takeSnapshot()</code>获取MonkeyImage对象</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div></pre></td><td class="code"><pre><div class="line"><span class="function">string	<span class="title">convertToBytes</span> <span class="params">(string format)</span></span></div><div class="line"><span class="function"><span class="comment">// Converts the current image to a particular format and returns it as a string that you can then access as an iterable of binary bytes.</span></span></div><div class="line"><span class="function"><span class="comment">// 转换格式</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function">tuple	<span class="title">getRawPixel</span> <span class="params">(integer x, integer y)</span></span></div><div class="line"><span class="function"><span class="comment">// Returns the single pixel at the image location (x,y), as an a tuple of integer, in the form (a,r,g,b).</span></span></div><div class="line"><span class="function"><span class="comment">// 获取像素值</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function">integer	<span class="title">getRawPixelInt</span> <span class="params">(integer x, integer y)</span></span></div><div class="line"><span class="function"><span class="comment">// Returns the single pixel at the image location (x,y), as a 32-bit integer.</span></span></div><div class="line"><span class="function"><span class="comment">// 获取像素值</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function">MonkeyImage	<span class="title">getSubImage</span> <span class="params">(tuple rect)</span></span></div><div class="line"><span class="function"><span class="comment">// Creates a new MonkeyImage object from a rectangular selection of the current image.</span></span></div><div class="line"><span class="function"><span class="comment">// 获取像素值</span></span></div><div class="line"><span class="function"><span class="comment">// 根据举行获取一个新的对象</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">sameAs</span> <span class="params">(MonkeyImage other, <span class="keyword">float</span> percent)</span></span></div><div class="line"><span class="function"><span class="comment">// Compares this MonkeyImage object to another and returns the result of the comparison. The percent argument specifies the percentage difference that is allowed for the two images to be "equal".</span></span></div><div class="line"><span class="function"><span class="comment">// 比较2各图片是相同</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">writeToFile</span> <span class="params">(string path, string format)</span></span></div><div class="line"><span class="function"><span class="comment">// Writes the current image to the file specified by filename, in the format specified by format.</span></span></div><div class="line"><span class="function"><span class="comment">// 保存图片</span></span></div></pre></td></tr></table></figure>
<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><h2 id="优点："><a href="#优点：" class="headerlink" title="优点："></a>优点：</h2><p>Monkeyrunner可以运行多个device，能够截屏，能够提供按键和输入事件。可以基于python进行扩展开发，功能强大。</p>
<h2 id="缺点："><a href="#缺点：" class="headerlink" title="缺点："></a>缺点：</h2><p>Monkeyrunner获取控件是根据坐标进行定位的，不同的设备的坐标不一样，获取不稳定。</p>
<h2 id="Monkey与Monkeyrunner差别"><a href="#Monkey与Monkeyrunner差别" class="headerlink" title="Monkey与Monkeyrunner差别"></a>Monkey与Monkeyrunner差别</h2><ul>
<li>Monkey<br>Monkey工具直接运行在设备或模拟器的adb shell中，生成用户或系统的伪随机事件流。</li>
<li>Monkeyrunner<br>Monkeyrunner工具是在工作站上通过API定义的特定命令和事件控制设备或模拟器。</li>
</ul>
<h1 id="相关链接"><a href="#相关链接" class="headerlink" title="相关链接"></a>相关链接</h1><p><a href="https://developer.android.com/studio/test/monkeyrunner/index.html" target="_blank" rel="external">https://developer.android.com/studio/test/monkeyrunner/index.html</a><br><a href="http://www.cnblogs.com/findyou/p/3420936.html" target="_blank" rel="external">http://www.cnblogs.com/findyou/p/3420936.html</a></p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechat_pay.jpeg" alt="流水不腐小夏 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.jpeg" alt="流水不腐小夏 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Android测试/" rel="tag"># Android测试</a>
          
            <a href="/tags/自动化测试/" rel="tag"># 自动化测试</a>
          
            <a href="/tags/Android-Monkey-Runner/" rel="tag"># Android Monkey Runner</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/07/19/Android-Monkey/" rel="next" title="Android Monkey整理">
                <i class="fa fa-chevron-left"></i> Android Monkey整理
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/07/25/Android-UIAutomator/" rel="prev" title="Android UIAutomator">
                Android UIAutomator <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/avatar.jpeg"
                alt="流水不腐小夏" />
            
              <p class="site-author-name" itemprop="name">流水不腐小夏</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">31</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">55</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          <div class="links-of-author motion-element">
            
              
                <span class="links-of-author-item">
                  <a href="https://github.com/LiushuiXiaoxia" target="_blank" title="GitHub">
                    
                      <i class="fa fa-fw fa-GitHub"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="http://www.jianshu.com/u/58771f980bac" target="_blank" title="简书">
                    
                      <i class="fa fa-fw fa-简书"></i>简书</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="http://blog.csdn.net/guijiaoba" target="_blank" title="CSDN">
                    
                      <i class="fa fa-fw fa-CSDN"></i>CSDN</a>
                </span>
              
            
          </div>

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Android-monkeyrunner整理"><span class="nav-number">1.</span> <span class="nav-text">Android monkeyrunner整理</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#简介"><span class="nav-number">2.</span> <span class="nav-text">简介</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#特性"><span class="nav-number">3.</span> <span class="nav-text">特性</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#使用方法"><span class="nav-number">4.</span> <span class="nav-text">使用方法</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Monkeyrunner-API"><span class="nav-number">4.1.</span> <span class="nav-text">Monkeyrunner API</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#运行"><span class="nav-number">4.2.</span> <span class="nav-text">运行</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#方法一"><span class="nav-number">4.3.</span> <span class="nav-text">方法一</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#方法二"><span class="nav-number">4.4.</span> <span class="nav-text">方法二</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#API"><span class="nav-number">5.</span> <span class="nav-text">API</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#MonkeyRunner"><span class="nav-number">5.1.</span> <span class="nav-text">MonkeyRunner</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#MonkeyDevice"><span class="nav-number">5.2.</span> <span class="nav-text">MonkeyDevice</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#MonkeyImage"><span class="nav-number">5.3.</span> <span class="nav-text">MonkeyImage</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#总结"><span class="nav-number">6.</span> <span class="nav-text">总结</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#优点："><span class="nav-number">6.1.</span> <span class="nav-text">优点：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#缺点："><span class="nav-number">6.2.</span> <span class="nav-text">缺点：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Monkey与Monkeyrunner差别"><span class="nav-number">6.3.</span> <span class="nav-text">Monkey与Monkeyrunner差别</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#相关链接"><span class="nav-number">7.</span> <span class="nav-text">相关链接</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2015 &mdash; <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">流水不腐小夏</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.3</div>




        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  

  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.3"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.3"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.3"></script>



  


  

    
      <script id="dsq-count-scr" src="https://mycommons-cn.disqus.com/count.js" async></script>
    

    
      <script type="text/javascript">
        var disqus_config = function () {
          this.page.url = 'http://mycommons.cn/2016/07/25/AndroidMonkeyRunner/';
          this.page.identifier = '2016/07/25/AndroidMonkeyRunner/';
          this.page.title = 'AndroidMonkeyRunner';
        };
        var d = document, s = d.createElement('script');
        s.src = 'https://mycommons-cn.disqus.com/embed.js';
        s.setAttribute('data-timestamp', '' + +new Date());
        (d.head || d.body).appendChild(s);
      </script>
    

  




	





  














  




  
  
  
  <link rel="stylesheet" href="/lib/algolia-instant-search/instantsearch.min.css">

  
  
  <script src="/lib/algolia-instant-search/instantsearch.min.js"></script>
  

  <script src="/js/src/algolia-search.js?v=5.1.3"></script>



  

  

  
  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  

</body>
</html>
